/*
Copyright 2011 Raymond Giorgi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

package edu.pitt.cs3530.regression;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import edu.pitt.cs3530.Constants;
import edu.pitt.cs3530.configuration.FrontEndConfiguration;
import edu.pitt.cs3530.controllers.ReactorListener;
import edu.pitt.cs3530.frontend.DeleteOldFilesThread;
import edu.pitt.cs3530.reactors.ReactorThread;
import edu.pitt.cs3530.reactors.ReactorMadeMissed;
import edu.pitt.cs3530.regression.TimeStamp.SecMSec;

public class Reactive implements ReactorListener {
	BufferedWriter writer = null;

	// (String directory, int deadline,
	// double allMadeBound, long sleepTime)
	public Reactive(String directory) {
		DeleteOldFilesThread doft = new DeleteOldFilesThread(directory, 1000,
				60 * 1000);
		doft.start();
		ReactorThread rt = new ReactorMadeMissed(directory, 1, .2, 100);
		rt.addListener(this);
		rt.start();
		for (int i = 0; i < 10000; i++) {
			try {
				long start = System.currentTimeMillis();
				this.writeFiles(this.getTestOne(directory, 1000));
				System.out.println("Write time:"
						+ (System.currentTimeMillis() - start));
				Thread.sleep(500);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static void main(String args[]) {
		String directory = "c:\\cpuUtilTemp\\";
		if (args.length > 0) {
			directory = args[0];
		}
		new Reactive(directory);
	}

	public void writeFiles(List<String> fileNames) throws IOException {
		FileWriter fw = null;
		BufferedWriter bw = null;
		// Mutexs.lockFileMutex();
		for (String fileName : fileNames) {
			long time = System.currentTimeMillis();
			fw = new FileWriter(fileName);
			bw = new BufferedWriter(fw);
			bw.write("");
			bw.close();
		}
		// writer.write("" + fileNames.size() + ":\r\n");
		// Mutexs.unlockFileMutex();
	}

	public List<String> getTestOne(String directory, int numberToProduce) {
		List<String> fileNames = new LinkedList<String>();
		Random rand = new Random();
		String fileName = "";
		for (int i = 0; i < numberToProduce; i++) {
			SecMSec secMSec = TimeStamp.createSecUSec();
			fileName = directory + "fin-" + secMSec.getSec() + "-"
					+ getRandomUSec(secMSec.getMSec()) + "-";
			long finSec = secMSec.getSec();
			long finMSec = secMSec.getMSec() + Math.abs(rand.nextInt() % 1000);
			if (finMSec > 999) {
				finMSec -= 1000;
				finSec++;
			}
			fileName += finSec + "-" + getRandomUSec(finMSec);
			fileNames.add(new String(fileName));
			// try {
			// Thread.sleep(Math.abs(rand.nextLong() % 1000));
			// } catch (InterruptedException e) {
			// // TODO Auto-generated catch block
			// e.printStackTrace();
			// }
			fileName = "";
		}
		return fileNames;
	}

	public long getRandomUSec(long mSec) {
		return (mSec * 1000) + Math.abs((new Random()).nextLong() % 1000);
	}

	@Override
	public void alertResponseListener(ReactorThread rt) {
		System.out.println(System.currentTimeMillis());
		System.out.println("\tMade:" + rt.getMade());
		System.out.println("\tMissed:" + rt.getMissed());
		System.out.println("\tMadeInBound:" + rt.getMadeInLowerDeadline());
		System.out.println("\tTotal:" + rt.getTotal());
		System.out.println("\tMade%:" + rt.getPercentMade());
		System.out.println("\tMissed%:" + rt.getPercentMissed());
		System.out.println("\tMadeInBound%:"
				+ rt.getPercentMadeInLowerDeadline());
		if (rt.getPercentMissed() > FrontEndConfiguration.getInstance()
				.getLateThresshold()) {
			System.out.println("SCALE: UP");
		}
	}
}
